// pages/search-result/search-result.js
import api from '../../utils/request/api'
const util = require('../../utils/util.js');
import {
  goodsList
} from '../../utils/request/apiurl'


Page({

  data: {
	goodList:[],
	searchGood: {
		page: 1,
		size: 20,
		pages:0
	},
	isRequest: false,
	rank: 0,//会员等级
	uid:'',
	id:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	  // console.log(options)
	  var userInfo = wx.getStorageSync('userInfo');
	  if (userInfo.rank) {
	  	this.setData({
	  		uid:userInfo.uid,
	  		rank: userInfo.rank,
	  	})
	  }
	  this.setData({
		  id:options.id//inputVal
	  })
	this.searchList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
  	wx.showNavigationBarLoading() //在标题栏中显示加载
	this.setData({
		goodList:[],
		searchGood: {
			page: 1,
			size: 20,
			pages:0
		},
	})
  	this.searchList()
  	setTimeout(function() {
  		wx.hideNavigationBarLoading() //完成停止加载
  		wx.stopPullDownRefresh() //停止下拉刷新
  	}, 1500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
	if (!this.data.isRequest && this.data.searchGood.page < this.data.searchGood.pages) {
		this.setData({
		  searchGood: {
		    page: this.data.searchGood.page + 1,
		    size: 10,
		    pages: this.data.searchGood.pages
		  }
		})
		this.searchList()
	}
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  focusSearch(){
	  wx.redirectTo({ url: '../good-search/good-search' })
  },
  // 搜索列表
  searchList(){
  	var source={
		uid:this.data.uid,
  		page: this.data.searchGood.page,
  		size: this.data.searchGood.size,
  		key:this.data.id,
  	}
  	api.post(goodsList.list, source).then(res => {
		if(res.length!=0){
			for(var i=0;i<res.rows.length;i++){
				// if(this.data.rank==1){
				// 	res.rows[i]['goodPrice']=res.rows[i].commonMember;
				// }else if(this.data.rank==2){
				// 	res.rows[i]['goodPrice']= res.rows[i].middleMember;
				// }else if(this.data.rank==3){
				// 	res.rows[i]['goodPrice']= res.rows[i].expertMember;
				// }else{
					res.rows[i]['goodPrice']= res.rows[i].currentPrice;
				// }
			}
		}else{
			wx.showToast({
				title: '请重新搜索!',
				icon: 'none'
			})
		}
		
		// console.log(res)
		this.setData({
			goodList:res.rows
		})
		let pages = res.total / this.data.searchGood.size
		this.setData({
		  searchGood: {
		    page: this.data.searchGood.page,
		    size: this.data.searchGood.size,
		    pages: pages
		  }
		})
  	}).catch(err => {
  		// console.log(err)
  		wx.showToast({
  			title:'请重新搜索!',
  			icon: 'none'
  		})
  	})	
  },
})